package com.ihrm.modules.sys.controller;

import com.ihrm.common.utils.PageUtils;
import com.ihrm.common.utils.R;
import com.ihrm.modules.sys.entity.IndustryEntity;
import com.ihrm.modules.sys.service.IndustryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 2017年三级行业分类表
 * @author gaogzhen
 * @email gaogzhen@foxmail.com
 * @date 2020-11-28 11:19:15
 */
@RestController
@RequestMapping("sys/industry")
@Api(tags = "Industry")
public class IndustryController {
    @Autowired
    private IndustryService industryService;

    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("列表")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = industryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询")
    public R info(@PathVariable("id") Integer id) {
        IndustryEntity industry = industryService.getById(id);

        return R.ok().put("industry", industry);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("添加")
    public R save(@RequestBody IndustryEntity industry) {
        industryService.save(industry);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public R update(@RequestBody IndustryEntity industry) {
        // ValidatorUtils.validateEntity(industry);
        industryService.updateById(industry);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @ApiOperation("删除")
    public R delete(@RequestBody Integer[] ids) {
        industryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 查询自己数据
     * @param id    父级id
     * @return      子级数据集合
     */
    @GetMapping("/subData/{id}")
    @ApiOperation("查询子级数据")
    public R subData(@PathVariable Integer id) {
        List<IndustryEntity> industries = industryService.subData(id);

        return R.ok().put("industries", industries);
    }
}
